/* global Cesium viewer */
export default class LayerManager {
	constructor(viewer) {
		this.viewer = viewer
		this.pointPrimitive = viewer.scene.primitives.add(new Cesium.PointPrimitiveCollection())
		this.polymericDataSource = null
		this.billboards = viewer.scene.primitives.add(
			new Cesium.BillboardCollection({
				scene: viewer.scene
			})
		)
		this.currentGeoJson = null
		this.init()
		this.Tile = null
		this.vecTile = null
	}
	init() {
		this.viewer.scene.globe.showGroundAtmosphere = false //关闭地面大气圈
		this.viewer.scene.skyAtmosphere.show = false //关闭天空大气圈
		let handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas)
		handler.setInputAction(function(movement) {
			let pickedObject = viewer.scene.pick(movement.position)

			if (Cesium.defined(pickedObject) && Cesium.defined(pickedObject.id) && typeof pickedObject.id.leftClick === 'function') {
				pickedObject.id.leftClick(pickedObject.id)
			}
		}, Cesium.ScreenSpaceEventType.LEFT_CLICK)
	}
	getCenter() {
		let centerx = (this.viewer.camera.positionCartographic.longitude * 180) / Math.PI
		let centery = (this.viewer.camera.positionCartographic.latitude * 180) / Math.PI
		let height = Math.ceil(this.viewer.camera.positionCartographic.height)
		let heading = this.viewer.camera.heading
		let pitch = this.viewer.camera.pitch
		let roll = this.viewer.camera.roll
		return centerx + ',' + centery + ',' + height + ',' + heading + ',' + pitch + ',' + roll
	}
	fly(lon, lat, alt, heading, pitch, roll, duration = 0.1) {
		this.viewer.camera.flyTo({
			destination: Cesium.Cartesian3.fromDegrees(lon, lat, alt), // 设置位置
			orientation: {
				heading: heading, // 方向
				pitch: pitch, // 倾斜角度
				roll: roll
			},
			duration: duration
		})
	}
	remove3dTiles() {
		if (this.Tile !== null) {
			viewer.scene.primitives.remove(this.Tile)
			this.Tile = null
		}
		if (this.vecTile !== null) {
			viewer.scene.primitives.remove(this.vecTile)
			this.vecTile = null
		}
		// viewer.scene.primitives.removeAll()
	}
	add3dTiles(url) {
		let tilesetModel = new Cesium.Cesium3DTileset({
			url
		})
		tilesetModel.readyPromise
			.then(function(argument) {
				viewer.scene.primitives.add(tilesetModel)
				viewer.zoomTo(tilesetModel)
			})
			.otherwise(function(error) {})
		return tilesetModel
	}
}
